package com.example.demo.service.serviceImp;

import com.example.demo.entity.Position;
import com.example.demo.mapper.PositionMapper;
import com.example.demo.outstyle.*;
import com.example.demo.service.PositionService;
import com.example.demo.util.MyPair;
import com.example.demo.util.UUidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PositionServiceImp implements PositionService {

    @Autowired
    private PositionMapper positionMapper;

    private int page = 1;
    /**
     * 获取职位总数，公司总数，平均薪资
     * @return
     */
    @Override
    public Base selectBaseData() {
        Base base = positionMapper.selectBaseData();
        return base;
    }

    /**
     * 获取当前语言的招聘量
     * 5个元素分别对应: Java，PHP，C++，iOS，Python
     * @return
     */
    @Override
    public List<Integer> selectLanguageCounts() {
        return positionMapper.selectLanguageCounts();
    }

    /**
     * 获取每个城市的职位数
     * @return
     */
    @Override
    public  List<MyPair<String, List<String>>> selectPositionByCity() {
        List<CityMapBase> tmp = positionMapper.selectPositionByCity();
        List<MyPair<String, List<String>>> result = new ArrayList<>();

        for (CityMapBase t : tmp) {
            MyPair<String, List<String>> mp = new MyPair<>();
            mp.setName(t.getName());
            mp.setValue(new ArrayList<String> (){ {add(t.getLongitude()); add(t.getLatitude()); add(t.getCount());}} );
            result.add(mp);
        }

        return result;
    }

    /**
     * 获取不同工作年限的平均薪资数据
     * 6个元素分别对应: 不限、应届毕业生、 3年以下、 3-5年、5-10年、10年以上
     * @return
     */
    @Override
    public List<Integer> selectSalaryByWorkYear() {
        return positionMapper.selectSalaryByWorkYear();
    }

    /**
     * 返回公司福利中，提及数量位于前20位的福利关键词
     * @return
     */
    @Override
    public List<MyPair<String, Integer>> selectComponyWelfare() {
        List<MyPair<String, Integer>> result = new ArrayList<>();

        HashMap<String, Integer> process = new HashMap<>();

        List<String> fuli = positionMapper.selectAdvantageWords();
        for (String f : fuli) {
            String[] newf = f.split(" +");
            for (String newfelement : newf) {
                newfelement = newfelement.trim();
                Integer tmp = process.get(newfelement);
                if (tmp == null) {
                    tmp = new Integer(0);
                }
                process.put(newfelement, tmp + 1);
            }
        }
        process.entrySet()
                .stream()
                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
                .limit(20)
                .collect(Collectors.toMap(
                        HashMap.Entry::getKey,
                        HashMap.Entry::getValue,
                        (oldValue, newValue) -> oldValue, LinkedHashMap::new
                        )
                ).forEach((k, v) -> result.add(new MyPair<>(k, v)));
        return result;
    }

    /**
     * 查询全部信息
     * @return
     */
    @Override
    public List<Position> selectAll(int page1,int limit,Position position) {
        int begin = limit * (page1-1);
        return positionMapper.selectAll(begin,limit,position);
    }

    /**
     * 删除招聘信息
     * @return
     */
    @Override
    public int deleteInfo(String id) {
        return positionMapper.deleteById(id);
    }

    /**
     * 更新招聘信息
     * @param position
     * @return
     */
    @Override
    public int updateInfo(Position position) {
        return positionMapper.updateById(position);
    }

    /**
     * 插入招聘信息
     * @param position
     * @return
     */
    @Override
    public int insertInfo(Position position) {
        position.setId(UUidUtil.getUUid());
        return positionMapper.insert(position);
    }

    /**
     * 查询招聘信息总数
     * @return
     */
    @Override
    public int selectPositionCount() {

        return positionMapper.selectCount(null);
    }

    /**
     * 职业类型通过firstType、secondType和thirdType形成了一个树形结构。
     * 返回整个树形结构。
     * @return
     */
    @Override
    public List<TreeType<TreeType<TreeTreeType>>> getType() {
        List<TreeType<TreeType<TreeTreeType>>> result = new LinkedList<>();
        List<Position> positions = positionMapper.selectAllType();

        Map<Integer, Boolean> check = new HashMap<>();

        Map<String, LinkedList<String> > need = new HashMap<>();
        List<String> fType = new LinkedList<>();
        MyPair<String, String> key = new MyPair<>();


        for (Position position : positions) {
            String fr = position.getFirstType(), se = position.getSecondType(), tr = position.getThirdType();
            if (fr == null || se == null || tr == null) continue;

            key.setAll(fr, se);
            int keyValue = key.hashCode();

            if (!check.containsKey(keyValue)) {

                LinkedList<String> tmp = need.get(fr);

                if (tmp == null) {
                    fType.add(fr);
                    tmp = new LinkedList<>();
                    tmp.add(se);
                    need.put(fr, tmp);
                } else tmp.add(se);

                check.put(keyValue, true);
            }

            key.setAll(se, tr);
            keyValue = key.hashCode();

            if (!check.containsKey(keyValue)) {
                LinkedList<String> tmp = need.get(se);
                if (tmp == null) {
                    tmp = new LinkedList<>();
                    tmp.add(tr);
                    need.put(se, tmp);
                } else tmp.add(tr);

                check.put(keyValue, true);
            }
        }

        for (String s : fType) {
            TreeType<TreeType<TreeTreeType>> tmp1 = new TreeType<>();
            tmp1.setLabel(s); tmp1.setValue(s);

            List <String> t2 = need.get(s);
            for (String s2 : t2) {

                List<String> t3 = need.get(s2);

                TreeType<TreeTreeType> tmp2 = new TreeType<>();
                tmp2.setValue(s2);
                tmp2.setLabel(s2);

                for (String s3 : t3) {
                    tmp2.getChildren().add(new TreeTreeType(s3, s3));
                }

                tmp1.getChildren().add(tmp2);
            }
            result.add(tmp1);
        }
        return result;
    }

    /**
     * 返回当前分支下的地区列表
     * @return
     */
    @Override
    public List<TreeType<TreeTreeType>> getLocation() {
        List<TreeType<TreeTreeType>> result = new LinkedList<>();
        List<Position> positions = positionMapper.selectAllLocation();

        Map<String, List<String>> need = new HashMap<>();
        Map<Integer, Boolean> check = new HashMap<>();

        for (Position p : positions) {
            String city = p.getCity(), dist = p.getDistrict();

            if (city == null || dist == null) continue;

            int keyValue = dist.hashCode();

            if (!check.containsKey(keyValue)) {

                List<String> tmp = need.get(city);
                if (tmp == null) {
                    tmp = new LinkedList<>();
                    tmp.add(dist);
                    need.put(city, tmp);
                } else tmp.add(dist);

                check.put(keyValue, true);
            }
        }
        for (Map.Entry<String, List<String>> entry : need.entrySet()) {
            TreeType<TreeTreeType> t1 = new TreeType<>();
            t1.setLabel(entry.getKey());t1.setValue(entry.getKey());
            entry.getValue().forEach(val -> { t1.getChildren().add(new TreeTreeType(val, val));});
            result.add(t1);
        }
        return result;
    }

    /**
     * 获取招聘信息总数
     * @return
     */
    @Override
    public int selectAllCount(int begin, int limit, Position example) {
        return positionMapper.selectAllCount(begin, limit, example);
    }

    @Override
    public Famous selectByFamous() {
        List<ByFamous> byFamous = positionMapper.selectByFamous();
        Famous result = new Famous();

        List<Integer> company = new ArrayList<>();
        List<Integer> salary = new ArrayList<>();
        List<List<Integer>> lists = new ArrayList<>();

        if (byFamous.size() == 0) {
            return new Famous();
        }
        if (byFamous.size() == 1) {
            ByFamous byNoFamous = positionMapper.selectByNoFamous();

            if (byNoFamous.getCompanyByFamous() == 0) {
                company.add(byFamous.get(0).getCompanyByFamous());
                company.add(0);


                salary.add(byFamous.get(0).getSalaryByFamous());
                salary.add(0);


                lists.add(positionMapper.selectWorkYearByFamous(1));
                lists.add(new ArrayList<>());
            } else {

                company.add(0);
                company.add(byFamous.get(0).getCompanyByFamous());

                salary.add(0);
                salary.add(byFamous.get(0).getSalaryByFamous());

                lists.add(new ArrayList<>());
                lists.add(positionMapper.selectWorkYearByFamous(0));
            }

            result.setCompanyByFamous(company);
            result.setSalaryByFamous(salary);
            result.setWorkyearByFamous(lists);
            return result;
        }
        company.add(byFamous.get(0).getCompanyByFamous());
        company.add(byFamous.get(1).getCompanyByFamous());
        result.setCompanyByFamous(company);

        salary.add(byFamous.get(0).getSalaryByFamous());
        salary.add(byFamous.get(1).getSalaryByFamous());
        result.setSalaryByFamous(salary);

        lists.add(positionMapper.selectWorkYearByFamous(1));
        lists.add(positionMapper.selectWorkYearByFamous(0));
        result.setWorkyearByFamous(lists);

        return result;
    }


    /**
     * 获取二十个招聘企业logo
     * @return
     */
    @Override
    public List<String> selectCompanyLogo(){
        //分页查询每次20条，页码加一
        int limit = 20;
        int begin = limit*(page - 1);
        List<String> companyLogo = positionMapper.selectCompanyLogo(begin,limit);
        page ++;
        return companyLogo;
    }//企业Logo轮播
    /**
     * 获取不同开发语言招聘数量
     * @return
     */
    @Override
    public List<Integer> selectPositionByLanguage(){
        List<Integer> positionByLanguage = positionMapper.selectPositionByLanguage();
        return positionByLanguage;
    }//开发语言招聘量
    /**
     * 获取薪资分布
     * @return
     */
    @Override
    public List<Integer> selectPositionBySalary(){
        Cnt cnt = positionMapper.selectPositionBySalary();
        List<Integer> positionBySalary = new ArrayList<>();
        positionBySalary.add(cnt.getCnt0());
        positionBySalary.add(cnt.getCnt1());
        positionBySalary.add(cnt.getCnt2());
        positionBySalary.add(cnt.getCnt3());
        positionBySalary.add(cnt.getCnt4());
        positionBySalary.add(cnt.getCnt5());
        positionBySalary.add(cnt.getCnt6());
        return positionBySalary;
    }//薪资分布

    @Override
    public List<Integer> selectPositionBySalaryTest() {
        List<Integer> salaryList = positionMapper.selectPositionBySalaryTest();
        List<Integer> result = Arrays.asList(0,0,0,0,0,0);
        for (Integer salary:salaryList){
            if (salary <= 5){
                result.set(0,result.get(0) + 1);
            }else if (salary > 5&&salary <= 10){
                result.set(1,result.get(1) + 1);
            }else if (salary > 10&&salary <= 15){
                result.set(2,result.get(2) + 1);
            }else if (salary > 15&&salary <= 20){
                result.set(3,result.get(3) + 1);
            }else if (salary > 20&&salary <= 25){
                result.set(4,result.get(4) + 1);
            }else{
                result.set(5,result.get(5) + 1);
            }
        }
        return result;
    }

    /**
     * 获取企业规模分布
     * @return
     */
    @Override
    public List<Integer> selectCompanyBySize(){
        List<Integer> companyBySize = positionMapper.selectCompanyBySize();
        return companyBySize;
    }//企业规模分布
    /**
     * 获取招聘量最多的地区
     * @return
     */
    @Override
    public List<District> selectTop5ByDistrict(){
        List<District> top5ByDistrict = positionMapper.selectTop5ByDistrict();
        return top5ByDistrict;
    }//行政区划Top5
    /**
     * 获取不同学历招聘信息
     * @return
     */
    @Override
    public List<Integer> selectPositionByEducation(){
        List<Integer> positionByEducation = positionMapper.selectPositionByEducation();
        return positionByEducation;
    }//学历要求
    /**
     * 根据关键词获取公司信息
     * @return
     */
    @Override
    public List<String> selectCompany(List<String> keyWordList){
        return  positionMapper.selectCompany(keyWordList);
    }

    /**
     * 根据关键词获取职位信息
     * @return
     */
    @Override
    public List<String> selectPosition(List<String> keyWordList) {
        return positionMapper.selectPosition(keyWordList);
    }
}
